from app import app, db
from models.accounting import AccountingSubject

def init_accounting_subjects():
    """初始化默认会计科目"""
    with app.app_context():
        # 检查是否已存在科目
        if AccountingSubject.query.first():
            print("会计科目已存在，跳过初始化")
            return
            
        # 创建默认会计科目
        subjects_data = [
            {'code': '1001', 'name': '库存现金', 'subject_type': 'ASSET', 'is_leaf': True},
            {'code': '1002', 'name': '银行存款', 'subject_type': 'ASSET', 'is_leaf': True},
            {'code': '1101', 'name': '短期投资', 'subject_type': 'ASSET', 'is_leaf': True},
            {'code': '1121', 'name': '应收票据', 'subject_type': 'ASSET', 'is_leaf': True},
            {'code': '1122', 'name': '应收账款', 'subject_type': 'ASSET', 'is_leaf': True},
            {'code': '1123', 'name': '预付账款', 'subject_type': 'ASSET', 'is_leaf': True},
            {'code': '1221', 'name': '其他应收款', 'subject_type': 'ASSET', 'is_leaf': True},
            {'code': '1401', 'name': '材料采购', 'subject_type': 'ASSET', 'is_leaf': True},
            {'code': '1403', 'name': '原材料', 'subject_type': 'ASSET', 'is_leaf': True},
            {'code': '1405', 'name': '库存商品', 'subject_type': 'ASSET', 'is_leaf': True},
            {'code': '1501', 'name': '长期投资', 'subject_type': 'ASSET', 'is_leaf': True},
            {'code': '1601', 'name': '固定资产', 'subject_type': 'ASSET', 'is_leaf': True},
            {'code': '1602', 'name': '累计折旧', 'subject_type': 'ASSET', 'is_leaf': True},
            {'code': '1701', 'name': '无形资产', 'subject_type': 'ASSET', 'is_leaf': True},
            {'code': '2001', 'name': '短期借款', 'subject_type': 'LIABILITY', 'is_leaf': True},
            {'code': '2201', 'name': '应付票据', 'subject_type': 'LIABILITY', 'is_leaf': True},
            {'code': '2202', 'name': '应付账款', 'subject_type': 'LIABILITY', 'is_leaf': True},
            {'code': '2203', 'name': '预收账款', 'subject_type': 'LIABILITY', 'is_leaf': True},
            {'code': '2211', 'name': '应付职工薪酬', 'subject_type': 'LIABILITY', 'is_leaf': True},
            {'code': '2221', 'name': '应交税费', 'subject_type': 'LIABILITY', 'is_leaf': True},
            {'code': '2231', 'name': '应付利息', 'subject_type': 'LIABILITY', 'is_leaf': True},
            {'code': '2241', 'name': '其他应付款', 'subject_type': 'LIABILITY', 'is_leaf': True},
            {'code': '2501', 'name': '长期借款', 'subject_type': 'LIABILITY', 'is_leaf': True},
            {'code': '4001', 'name': '实收资本', 'subject_type': 'EQUITY', 'is_leaf': True},
            {'code': '4002', 'name': '资本公积', 'subject_type': 'EQUITY', 'is_leaf': True},
            {'code': '4101', 'name': '盈余公积', 'subject_type': 'EQUITY', 'is_leaf': True},
            {'code': '4103', 'name': '本年利润', 'subject_type': 'EQUITY', 'is_leaf': True},
            {'code': '4104', 'name': '利润分配', 'subject_type': 'EQUITY', 'is_leaf': True},
            {'code': '5001', 'name': '生产成本', 'subject_type': 'EXPENSE', 'is_leaf': True},
            {'code': '5101', 'name': '制造费用', 'subject_type': 'EXPENSE', 'is_leaf': True},
            {'code': '5201', 'name': '管理费用', 'subject_type': 'EXPENSE', 'is_leaf': True},
            {'code': '5301', 'name': '财务费用', 'subject_type': 'EXPENSE', 'is_leaf': True},
            {'code': '5401', 'name': '销售费用', 'subject_type': 'EXPENSE', 'is_leaf': True},
            {'code': '6001', 'name': '主营业务收入', 'subject_type': 'INCOME', 'is_leaf': True},
            {'code': '6051', 'name': '其他业务收入', 'subject_type': 'INCOME', 'is_leaf': True},
            {'code': '6101', 'name': '投资收益', 'subject_type': 'INCOME', 'is_leaf': True},
            {'code': '6301', 'name': '营业外收入', 'subject_type': 'INCOME', 'is_leaf': True},
            {'code': '6401', 'name': '主营业务成本', 'subject_type': 'EXPENSE', 'is_leaf': True},
            {'code': '6403', 'name': '营业税金及附加', 'subject_type': 'EXPENSE', 'is_leaf': True},
            {'code': '6501', 'name': '其他业务成本', 'subject_type': 'EXPENSE', 'is_leaf': True},
            {'code': '6601', 'name': '销售费用', 'subject_type': 'EXPENSE', 'is_leaf': True},
            {'code': '6602', 'name': '管理费用', 'subject_type': 'EXPENSE', 'is_leaf': True},
            {'code': '6603', 'name': '财务费用', 'subject_type': 'EXPENSE', 'is_leaf': True},
            {'code': '6701', 'name': '资产减值损失', 'subject_type': 'EXPENSE', 'is_leaf': True},
            {'code': '6711', 'name': '营业外支出', 'subject_type': 'EXPENSE', 'is_leaf': True},
            {'code': '6801', 'name': '所得税费用', 'subject_type': 'EXPENSE', 'is_leaf': True}
        ]
        
        for subject_data in subjects_data:
            subject = AccountingSubject(**subject_data)
            db.session.add(subject)
        
        db.session.commit()
        print("会计科目初始化完成")

if __name__ == "__main__":
    init_accounting_subjects()